/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vcardreader.data;

import vcardreader.constants.VCFConstants;

/**
 * @author jjophin
 */
public class Contact {
    private String name;
    private String firstName;
    private String lastName;
    private String fullName;
    private String[] numbers;
    
    public Contact(String vcfEntry) {
        String[] lines = vcfEntry.split(VCFConstants.REGEX_ENTRY_SPLITTER);
        name = lines[1];
        String[] names = getFormattedName().split(VCFConstants.REGEX_NAME_SPLITTER);
        firstName = names[0];
        lastName = names[1];
        fullName = lines[2];
        for(int i = 3; i < lines.length; i++) {
            numbers[i - 3] = lines[i];
        }
    }

    /**
     * Returns the full name string in the vCard format
     * @return name
     */    
    public String getFullName() {
        return fullName;
    }
    
    /**
     * Retruns the contact full name alone after replacing the FN: prefix
     * @return formatted full name
     */
    public String getFormattedFullName() {
        return fullName.replaceFirst(VCFConstants.CONTACT_FULL_NAME_PREFIX, "");
    }
    
    /**
     * Returns the name string in the vCard format
     * @return name
     */
    public String getName() {
        return name;
    }
    
    /**
     * Returns the formatted name after removing the N: prefix
     * @return 
     */
    private String getFormattedName() {
        return name.replaceFirst(VCFConstants.CONTACT_NAME_PREFIX, "");
    }
    
    /**
     * Retruns contact first name
     * @return 
     */
    public String getFirstName() {
        return firstName;
    }
    
    /**
     * Retruns contact last name
     * @return 
     */
    public String getLastName() {
        return lastName;
    }
    
    /**
     * Returns the array of phone number strings in the vCard format
     * @return name
     */    
    public String[] getNumbers() {
        return numbers;
    }
    
    /**
     * Retruns the phone number alone from a vcard phone number entry
     * @param phoneNumber
     * @return 
     */
    public String getFormattedNumber(String phoneNumber) {
        return phoneNumber.replaceFirst(VCFConstants.REGEX_TEL_NO, "");
    }
    
    /**
     * To add a new number of a particular type. 
     * @param phoneNumber - phone number to be added
     * @param type - type of the number to be added. Available in VCFConstants
     * @param isDefault - denotes whether the number is to be made the default number
     * @return true if the number is added. false if the number already exists
     */
    public boolean addPhoneNumber(String phoneNumber, String type, boolean isDefault) {
        boolean isDuplicate = checkDuplicateNumber(phoneNumber);
        if(!isDuplicate) {
            String numberString = VCFConstants.CONTACT_TEL_NO_PREFIX + ";" + type + ";";
            numberString += isDefault ? (VCFConstants.TEL_TYPE_DEFAULT + ":") : ":";
            numberString += phoneNumber;
            numbers[numbers.length] = numberString;
            return true;
        }
        return false;
    }
    
    private boolean checkDuplicateNumber(String phoneNumber) {
        for(int i = 0; i < numbers.length; i++) {
            if(getFormattedNumber(phoneNumber) == phoneNumber) {
                return true;
            }
        }
        return false;
    }
}